package com.daxia.bumall.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.daxia.bumall.daobase.GenericDAOHibernate;
import com.daxia.bumall.dto.BrandStyleDTO;
import com.daxia.bumall.dto.HouseStyleDTO;
import com.daxia.bumall.model.HouseStyle;
import com.daxia.bumall.support.Page;

@Repository
public class HouseStyleDAO extends GenericDAOHibernate<HouseStyle> {

	public List<HouseStyle> findDistinctStyleFromBrand(BrandStyleDTO query) {
		StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append(" select distinct n.houseStyle From BrandStyle n where 1 = 1 ");
        if (query.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(query.getId());
        }
        if(query.getBrand() != null) {
        	if (StringUtils.isNotBlank(query.getBrand().getName())) {
                hql.append(" and n.brand.name = ? ");
                paras.add(query.getBrand().getName());
            }
        }
        hql.append(" and (n.houseStyle.deleted = 0 or n.houseStyle.deleted is null)");
        return super.find(hql.toString(), paras.toArray(), null);
	}
	
    public List<HouseStyle> find(HouseStyleDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From HouseStyle n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            hql.append(" and n.name = ? ");
            paras.add(dto.getName());
        }
        hql.append(" and (n.deleted = 0 or n.deleted is null)");
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public List<HouseStyleDTO> findBrandHouseStyle(){
    	String hql="select distinct b.houseStyle from BrandStyle b where b.houseStyle.id is not null ";
    	return super.find2(hql, null, null);
    }
    public HouseStyle findOne(HouseStyleDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<HouseStyle> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }
}
